package com.hqyj.springBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.account.entity.Role;
import com.hqyj.springBoot.modules.account.entity.User;
import com.hqyj.springBoot.modules.account.service.RoleService;
import com.hqyj.springBoot.modules.account.service.UserService;
import com.hqyj.springBoot.modules.common.entity.ResultEntity;
import com.hqyj.springBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 127.0.0.1/api/role ---- post
     * {"roleName":"管理员"}
     */
    @PostMapping(value = "/role", consumes = "application/json")
    @Transactional
    public ResultEntity<Role> addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }


    /**
     * 127.0.0.1/api/role ---- put
     * {"id":1,"roleName":"管理员"}
     */
    @PutMapping(value = "/role", consumes = "application/json")
    @Transactional
    public ResultEntity<Role> updateRoleByRole(@RequestBody Role role){
        return roleService.updateRoleByRole(role);
    }

    /**
     * 127.0.0.1/api/role/2 ---- delete
     */
    @DeleteMapping(value = "/role/{id}")
    public ResultEntity<Role> deleteRoleById(@PathVariable int id){
        return roleService.deleteRoleById(id);
    }

    /**
     *127.0.0.1/api/role/1 ---- get
     */
    @GetMapping("role/{id}")
    public Role selectRoleById(@PathVariable int id){
        return roleService.selectRoleById(id);
    }

    /**
     *127.0.0.1/api/roles ---- get
     */
    @GetMapping("roles")
    public List<Role> selectAllRole(){
        return roleService.selectAllRole();
    }

    /**
     * 127.0.0.1/api/roles ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/roles", consumes = "application/json")
    public PageInfo<Role> getRolesBySearchBean(@RequestBody SearchBean searchBean) {
        return roleService.getRolesBySearchBean(searchBean);
    }

}
